package com.g0dkar.leet.util.file;

import java.io.IOException;
import java.io.InputStream;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.g0dkar.leet.exception.LeetRuntimeException;
import com.g0dkar.leet.log.Log;

/**
 * Basic XML file opener.
 * 
 * @author daniel
 */
public final class XMLUtils {

	private static SAXBuilder builder = new SAXBuilder();
	
	private XMLUtils() throws InstantiationException {
		throw new InstantiationException("You cannot create an instance of this class.");
	}
	
	public static Element parse(String file) {
		InputStream stream = FileUtils.getResourceAsStream(file);
		if (stream == null) {
			Log.error("XML resource not found: " + file);
			return null;
		}
		try {
			return builder.build(stream).getRootElement();
		} catch (JDOMException e) {
			throw new LeetRuntimeException("XML parsing error with file " + file + " : " + e);
		} catch (IOException e) {
			throw new LeetRuntimeException("IO error when reading file " + file + " : " + e);
		}
	}
	
}
